import cv2
import numpy as np

image = cv2.imread('xingyun.jpg')

hsv = cv2.cvtColor(
    image,
    cv2.COLOR_BGR2HSV
)

lower_color = np.array([0, 100, 90])
hight_color = np.array([180, 255, 255])
color_mask = cv2.inRange(hsv, lower_color, hight_color)
# 区分黄色、红色和蓝色
# 黄色
lower_yellow = np.array([20, 100, 100])
upper_yellow = np.array([30, 255, 255])
yellow_mask = cv2.inRange(hsv, lower_yellow, upper_yellow)

# 红色
lower_red1 = np.array([0, 100, 100])
upper_red1 = np.array([10, 255, 255])
lower_red2 = np.array([160, 100, 100])
upper_red2 = np.array([180, 255, 255])
red_mask1 = cv2.inRange(hsv, lower_red1, upper_red1)
red_mask2 = cv2.inRange(hsv, lower_red2, upper_red2)
red_mask = cv2.bitwise_or(red_mask1, red_mask2)

# 蓝色
lower_blue = np.array([100, 100, 100])
upper_blue = np.array([140, 255, 255])
blue_mask = cv2.inRange(hsv, lower_blue, upper_blue)

replace_mask = cv2.bitwise_or(yellow_mask, red_mask)
replace_mask = cv2.bitwise_and(replace_mask, cv2.bitwise_not(blue_mask))

kernel = np.ones((2, 2), np.uint8)
replace_mask = cv2.morphologyEx(
    replace_mask,
    cv2.MORPH_CLOSE,
    kernel
)

blue_color = [255, 0, 0]
image[replace_mask > 0] = blue_color

# 墙体变白
# 识别灰白色区域
lower_gray = np.array([0, 0, 150])
upper_gray = np.array([180, 50, 255])
gray_mask = cv2.inRange(hsv, lower_gray, upper_gray)

# 变为更白的颜色
white_color = [255, 255, 255]
image[gray_mask > 0] = white_color

# 确保黑色菱形框架不被影响
# 识别黑色区域
lower_black = np.array([0, 0, 0])
upper_black = np.array([180, 255, 50])
black_mask = cv2.inRange(hsv, lower_black, upper_black)

# 恢复黑色框架
image[black_mask > 0] = [0, 0, 0]

cv2.imshow('result',image )
cv2.imwrite('xingyun1.jpg',image)

cv2.waitKey(0)